<%= page_title @deploy_group.name %>

<section class="form-horizontal">
  <div class="form-group">
    <label class="col-lg-2 control-label">$DEPLOY_GROUPS value</label>
    <div class="col-lg-4">
      <p class="form-control-static"><%= @deploy_group.env_value %></p>
    </div>
  </div>

  <div class="form-group">
    <label class="col-lg-2 control-label">Environment</label>
    <div class="col-lg-4">
      <p class="form-control-static"><%= link_to @deploy_group.environment.name, @deploy_group.environment %></p>
    </div>
  </div>

  <div class="form-group row">
    <label class="col-lg-2 control-label">Deployed</label>
    <div class="col-lg-10">
      <table class="table-condensed">
        <tr>
          <th>Project</th>
          <th>Version</th>
          <th></th>
        </tr>
        <% @deployed.each do |deploy| %>
          <tr>
            <td><%= link_to deploy.project.name, deploy.project %></td>
            <td><%= link_to deploy.short_reference, [deploy.project, deploy] %></td>
            <td><%= render_time(deploy.start_time) %></td>
          </tr>
        <% end %>
      </table>
    </div>
  </div>

  <div class="form-group row">
    <label class="col-lg-2 control-label">Used by</label>
    <div class="col-lg-10">
      <table class="table-condensed">
        <tr>
          <th>Project</th>
          <th>Stage</th>
        </tr>
        <% @deploy_group.deploy_groups_stages.group_by { |dgs| dgs.stage.project }.each do |project, dgs_group| %>
          <% dgs_group.each do |dgs| %>
            <tr>
              <td><%= link_to project.name, project %></td>
              <td><%= link_to dgs.stage.name, [dgs.stage.project, dgs.stage] %></td>
            </tr>
          <% end %>
        <% end %>
      </table>
    </div>
  </div>

  <% if Samson::Secrets::Manager.backend == Samson::Secrets::HashicorpVaultBackend %>
    <div class="form-group">
      <label class="col-lg-2 control-label">Vault Server</label>
      <div class="col-lg-4">
        <p class="form-control-static">
          <% if vault_server = @deploy_group.vault_server %>
            <%= link_to vault_server.name, vault_server %>
          <% end %>
        </p>
      </div>
    </div>
  <% end %>

  <%= Samson::Hooks.render_views(:deploy_group_show, self) %>

  <div class="form-group">
    <% if DeployGroup.enabled? %>
      <div class="col-lg-offset-2 col-lg-10" style="display: none" id="mass_rollout">
        <%= link_to "Create Stages ...",
            new_deploy_group_mass_rollouts_path(@deploy_group),
            class: "btn btn-default"
        %>
        <%= link_to "Deploy All Projects",
            deploy_deploy_group_mass_rollouts_path(@deploy_group),
            class: "btn btn-default",
            data: {
              method: "post",
              confirm: "Deploys will be started for all stages."
            }
        %>
        <%= link_to "Deploy Missing Projects",
            deploy_deploy_group_mass_rollouts_path(@deploy_group, missing_only: "true"),
            class: "btn btn-default",
            data: {
              method: "post",
              confirm: "Deploys will be started for any stages that haven't yet been deployed or have failed all their deploys."
            }
        %>
        <% cloned_stage_count = @deploy_group.stages.cloned.count %>
        <%= link_to "Merge #{cloned_stage_count} Cloned Stages",
            merge_deploy_group_mass_rollouts_path(@deploy_group),
            class: "btn btn-default #{ 'disabled' if cloned_stage_count.zero? }",
            data: {
              method: "post",
              confirm: "Stages will be deleted. Before deleting, each stage will have its Deploy Group added to the stage it was copied from. Stages affected are those cloned from a template stage, have exactly this deploy group, and are on a project that uses include_new_deploy_groups.\n\n" \
                "Merge #{@deploy_group.stages.cloned.count} cloned stages now?"
            }
        %>
        <%= link_to "Delete #{cloned_stage_count} Cloned Stages",
            deploy_group_mass_rollouts_path(@deploy_group),
            class: "btn btn-default #{ 'disabled' if cloned_stage_count.zero? }",
            data: {
              method: "delete",
              confirm: "Cloned stages will be deleted. No stages will be merged into their templates. You will need to create a new stage to deploy the project to this deploy group."
            }
        %>
      </div>
    <% end %>
    <br>
    <div class="col-lg-offset-2 col-lg-10" style="margin-top: 10px;">
      <%= link_to "Edit", edit_deploy_group_path(@deploy_group), class: "btn btn-primary" %>
      <%= link_to "Back", deploy_groups_path, class: "btn btn-default" %>
      | <%= link_to_history @deploy_group %>
      | <%= link_to "Deploys", deploys_path(search: {group: "DeployGroup-#{@deploy_group.id}"}) %>
      | <%= link_to "Mass rollout", "#", data: {target: "#mass_rollout"}, class: 'toggle' if DeployGroup.enabled? %>
      | <%= link_to "Missing config", missing_config_deploy_group_path(@deploy_group) %>
    </div>
  </div>
</section>
